var IndeterminateProgress = { };

//Displays an indeterminate progress animation
//and returns the runner "thread" so that it
//may be stopped whenever the action has finished,
//stopping the runner will not remove the appended
//image element
IndeterminateProgress.show = function(elem) {
    //Setup
    var LAST_IMAGE = 25;
    var SPEED = 120;
    
    var img = document.createElement("img");
    elem.append(img);
    
    //Create the runner
    var runner = { };
    runner.cont = true;
    runner.at = 1;
    
    runner.stop = function() {
	runner.cont = false;
    };
    
    //This is where the magic happens
    runner.rotate = function() {
	if (runner.cont) {
	    var delay = SPEED;
	    runner.at++;
	    if (runner.at == LAST_IMAGE) {
		runner.at = 1;
		delay *= 3;
	    }
	    
	    img.src = "images/" + runner.at + ".png";
	    setTimeout(runner.rotate, delay);
	}
    };
    
    runner.start = function() {
	runner.rotate();
    };
    
    runner.start();
    //Return the runner, so that it may be stopped
    return runner;  
};